package ui.faces
{
	import flash.display.Sprite;
	
	import global.Managers;
	
	import lang.LanguageManager;
	
	import ui.faces.management.ManagementFace;
	
	import utils.IObservable;
	import utils.IObserver;
	
	import worlds.BaseWorld;
	
	public class FaceManager implements IObserver
	{
		public var idleFace						: IdleFace;
		public var managementFace				: ManagementFace;
		public var kitchenFace					: KitchenFace;
		public var commonFace					: CommonFace;
		public var mainFace						: MainFace;
		public var diningFace					: DiningFace;
		
		private var _currentFace				: AbstractFace;
		
		public function get currentFace():AbstractFace {
			return _currentFace;
		}
		
		public function FaceManager()
		{
		}
		public function init(languageManager:LanguageManager):void {
			// 注册观察LanguageManager
			languageManager.addObserver(this);
			// 初始化所有界面
			var faceLayer:Sprite = BaseWorld.getInstance().faceLayer;
			
			idleFace = new IdleFace();
			managementFace = new ManagementFace();
			kitchenFace = new KitchenFace();
			commonFace = new CommonFace();
			mainFace = new MainFace();
			diningFace = new DiningFace();
			
			idleFace.init();
			managementFace.init();
			kitchenFace.init();
			commonFace.init();
			mainFace.init();
			diningFace.init();
			
			idleFace.visible = false;
			managementFace.visible = false;
			kitchenFace.visible = false;
			commonFace.visible = false;
			mainFace.visible = false;
			diningFace.visible = false;
			
			faceLayer.addChild(idleFace);
			faceLayer.addChild(managementFace);
			faceLayer.addChild(kitchenFace);
			faceLayer.addChild(commonFace);
			faceLayer.addChild(mainFace);
			faceLayer.addChild(diningFace);
		}
		
		public function updateObserver(observable:IObservable, arg:Object = null):void {
			// 当语言发生变化的时候会执行该方法，负责更新当前界面的语言
			if(_currentFace != null) {
				_currentFace.switchLanguage(Managers.languageManager.currentLanguage);
			}
		}
		public function switchTo(face:AbstractFace):void {
			if(face == null) return;
			// 其次切换界面
			if(face != _currentFace) {
				// 首先将语言切换掉
				face.switchLanguage(Managers.languageManager.currentLanguage);
				// 这里使用过渡动画
				if(_currentFace != null) _currentFace.visible = false;
				face.visible = true;
				face.reset();
				// 最后将当前界面设置为新界面
				_currentFace = face;
			}
		}
	}
}